const jwt = require("jsonwebtoken");
const secret = "hqf+wmx+lrz+hyl";
let createToken = (data, expiresIn) => {
    let obj = {};
    obj.data = data || {};
    obj.ctime = (new Date()).getTime();
    obj.expiresIn = 1000 * 60 * 60 * expiresIn;
    let token = jwt.sign(obj, secret);
    return token;
};
let varifyToken = (token) => {
    let result = null;
    try {
        let {
            data,
            ctime,
            expiresIn
        } = jwt.verify(token, secret);
        let nowTime = (new Date()).getTime();
        if (nowTime - ctime < expiresIn) {
            result = data;
        }
    } catch (error) {}
    return result;
};
module.exports = {
    createToken,
    varifyToken
};